import React, { useState, useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import axios from 'axios'
import baseurl from '../../axios/config'
import { Table,Button } from 'antd'
// import {ipcRenderer} from 'electron'

export default function Home() {
  // 声明路由跳转钩子
  const navigator = useNavigate()
  // 验证登录
  const token = localStorage.getItem('token')
  if (!token) {
    navigator('/reg')
  }
 
  const xiaolu = localStorage.getItem('xiaolu')


  const [fetchdata, setfetchdata] = useState([])
  const [status, setstatus] = useState('')
  const [startup, setstartup] = useState('')
// 进行数据处理

  useEffect(() => {
  const fatch= async()=>{
    await axios.get(`http://node.dayuan0307.top:300/zhihui/query/${xiaolu}`).then((res)=>{
      console.log(res.data)
     const  data = res.data
      setfetchdata(res.data)
    
    
    })
    
  }

    fatch()

    const da = setInterval(() => {
      fatch()
      // 15分钟提价数据
    },60000)

    return () => {
      clearInterval(da)
    }
  }, [])
  const columns = [
    
    {
      title: '业主姓名',
      dataIndex: 'name',
      key: 'name',
    },
    {
      title: '节点状态',
      dataIndex: 'status',
      key: 'status',
    },
    
    {
      title: '开机时间',
      dataIndex: 'startup',
      key: 'startup',
    },
    {
      title: '数据提交',
      dataIndex: 'time',
      key: 'time',
    },
  ];

 const  handuser=()=>{
 
    localStorage.removeItem("token")
    localStorage.removeItem("xiaolu")
    navigator('/')

  }
  return (
    <div  >
      <div style={{marginTop:30,}}>
      <h1 style={{color:"red",textAlign:"center"}}>本地pi业主:{xiaolu}</h1>
      </div>
      <div style={{marginTop:50}}>
    <Table columns={columns} dataSource={fetchdata}></Table> 
    </div>
    <div >
      <Button type='primary' onClick={handuser}>切换用户</Button>
    </div>
    <div style={{marginTop:30,}}>
      <h1    style={{color:"blue",textAlign:"center",fontSize:15}}>本软件版权属于刘晓陆</h1>
    </div>
    </div>
  )
}
